package com.bigData.main.service.impl;

import com.bigData.main.mapper.CarCountMapper;
import com.bigData.main.service.CarCountService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CarCountServiceImpl implements CarCountService {

    @Resource
    CarCountMapper carCountMapper;

    public List<Map<String, Object>> countCar() {
        return carCountMapper.countCar();
    }

    public List<Map<String, Object>> countColor() {
        return carCountMapper.countColor();
    }

    public List<Map<String, Object>> countPriceRange() {
        return carCountMapper.countPriceRange();
    }

    public List<Map<String, Object>> countMonthlySales() {
        return carCountMapper.countMonthlySales();
    }

    public List<Map<String, Object>> countUserQuantity() {
        return carCountMapper.countUserQuantity();
    }

    public List<Map<String, Object>> countUserPrice() {
        return carCountMapper.countUserPrice();
    }

    public Integer allUser() {
        return carCountMapper.allUser();
    }

    public Integer unallCar() {
        return carCountMapper.unAllCar();
    }

    public Integer allStorage() {
        return carCountMapper.allStorage();
    }

    public List<Map<String, Object>> allStorageCount() {
        return carCountMapper.allStorageCount();
    }

    public List<Map<String, Object>> countBrandRatio() {
        return carCountMapper.countBrandRatio().stream()
                .peek(map -> {
                    // 处理可能的 null 值
                    if (map.get("total") == null || map.get("sold") == null) {
                        map.put("ratio", 0.0);
                        return;
                    }

                    // 通用数值转换方案
                    Number totalNum = (Number) map.get("total");
                    Number soldNum = (Number) map.get("sold");

                    long total = totalNum.longValue();
                    long sold = soldNum.longValue();

                    double ratio = total > 0 ?
                            (sold * 100.0) / total : 0;
                    ratio = Math.round(ratio * 100) / 100.0; // 保留两位小数

                    map.put("ratio", ratio);
                })
                .collect(Collectors.toList());
    }

    @Override
    public Map<String, Object> getFinancialStats(String startTime, String endTime) {
        Map<String, Object> revenueMap = carCountMapper.calculateRevenue(startTime, endTime);
        Map<String, Object> expensesMap = carCountMapper.calculateExpenses(startTime, endTime);

        double revenue = 0.0;
        if (ObjectUtils.isNotEmpty(revenueMap)) {
            revenue = revenueMap.get("total") != null ? Double.parseDouble(revenueMap.get("total").toString()) : 0;
        }

        double expenses = 0.0;
        if (ObjectUtils.isNotEmpty(expensesMap)) {
            expenses = expensesMap.get("total") != null ? Double.parseDouble(expensesMap.get("total").toString()) : 0;
        }

        double profit = revenue - expenses;

        Map<String, Object> result = new HashMap<>();
        result.put("revenue", revenue);
        result.put("expenses", expenses);
        result.put("profit", profit);

        return result;
    }

}
